/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.huiyun.project.pay.biz.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiyun.project.common.security.util.AssertUtils;
import com.huiyun.project.pay.biz.consts.ErrorConst;
import com.huiyun.project.pay.biz.entity.PayChannel;
import com.huiyun.project.pay.biz.entity.PayOrder;
import com.huiyun.project.pay.biz.mapper.PayChannelMapper;
import com.huiyun.project.pay.biz.service.PayChannelService;
import com.huiyun.project.pay.biz.service.PayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 支付渠道
 *
 * @author lengleng
 * @date 2019-05-28 23:58:18
 */
@Service
public class PayChannelServiceImpl extends ServiceImpl<PayChannelMapper, PayChannel> implements PayChannelService {
    @Autowired
    private PayOrderService payOrderService;

    /**
     * 通过商户和渠道Id查询渠道配置信息
     * 传递商户id是为了做校验,channelAppId是唯一的,单个其实就可以查询到渠道信息
     *
     * @param mchId
     * @param channelAppId
     * @return
     */
    @Override
    public PayChannel getByMchIdAndChannelAppId(String mchId, String channelAppId) {
        return baseMapper.selectOne(Wrappers.query(new PayChannel().setChannelAppId(channelAppId).setMchId(mchId)));
    }

    /**
     * 通过订单号和商户获取支付订单的渠道信息
     *
     * @param mchId
     * @param mchOrderNo
     * @return
     */
    @Override
    public PayChannel getByMchIdAndMchOrderNo(String mchId, String mchOrderNo) {
        PayOrder payOrder = payOrderService.getByMchOrderNo(mchId, mchOrderNo);
        AssertUtils.notNull(payOrder, ErrorConst.PAY_ORDER_IS_NULL, ErrorConst.PAY_ORDER_IS_NULL_TEXT);
        return getByMchIdAndChannelAppId(mchId, payOrder.getChannelAppId());
    }

    @Override
    public PayChannel getByChannelAppId(String appId) {
        return baseMapper.selectOne(Wrappers.query(new PayChannel().setChannelAppId(appId)));
    }
}
